package mx.com.fractal.ristorante.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import mx.com.fractal.ristorante.dao.MesaDAO;
import mx.com.fractal.ristorante.dto.MesaDTO;
import mx.com.fractal.ristorante.util.ParamSrcUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.stereotype.Repository;

@Repository
public class MesaDAOImpl implements MesaDAO{

	private static final String TABLA="ristorant_e.cat_mesa";
	private static final String INSERT = "INSERT INTO "+TABLA+"("
			+ "id_restaurante,num_mesa) "
			+ "values(:getIdRestaurante,:getNumMesa)";
	
	@Autowired
	private NamedParameterJdbcTemplate jdbc;
	
	public Integer save(MesaDTO mesa) {
		MapSqlParameterSource parameters = 
				ParamSrcUtil.getParams(mesa);
		GeneratedKeyHolder holder = new GeneratedKeyHolder();
		jdbc.update(INSERT, parameters, holder);
		
		return (Integer) holder.getKeys().get("id_mesa");
	}

	private static final String GET_ALL = "SELECT id_mesa,id_restaurante,num_mesa FROM "+TABLA
			+" WHERE id_restaurante = :idRest";
	public List<MesaDTO> getAllBy(MesaDTO mesa) throws SQLException {
		return null;
//		MapSqlParameterSource paramMap=new MapSqlParameterSource("idRest", idRestaurante);
//		return jdbc.query(GET_ALL, paramMap, new MesaRowMapper());
	}
	
	private class MesaRowMapper implements RowMapper<MesaDTO>{

		public MesaDTO mapRow(ResultSet rs, int i) throws SQLException {
			MesaDTO mesa=new MesaDTO();
			mesa.setIdMesa(rs.getInt("id_mesa"));
			mesa.setIdRestaurante(rs.getShort("id_restaurante"));
			mesa.setNumMesa(rs.getShort("num_mesa"));
			return mesa;
		}
		
	}

}
